package cn.asura.client;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * TCP: 客户端
 */
public class MyClient {
    public static void main(String[] args) throws Exception {
        // 端口, ip
        int port = 9999;
        String ip = "localhost";
        // String ip = "192.168.208.82";

        try (Socket socket = new Socket(ip, port);          // 创建客户端对象
             OutputStream os = socket.getOutputStream()) {  // 获取输出流

            String data = "你好，红娟";
            // 输出数据
            os.write(data.getBytes());

            // 接受服务端的响应
            byte[] bytes;
            try (InputStream is = socket.getInputStream()) {
                bytes = new byte[1024];
                @SuppressWarnings("unused")
                int read = is.read(bytes);
            }

            String s = new String(bytes, StandardCharsets.UTF_8);
            System.out.println("回信：" + s);
        }
    }
}
